package com.example.controller;

import com.example.pojo.LoginInfo;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) throws IOException {
        log.info("登录操作: {}", user);
        LoginInfo loginInfo = loginService.login(user);

        if (loginInfo != null) {
            return Result.success(loginInfo);
        }

        return Result.error("用户名或密码错误");
    }

}
